﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;

namespace WirtualneNauczanie.Web
{
    public partial class Course : System.Web.UI.Page
    {
        String yourcourseid;
        DropDownList DropDownList1, DropDownList2;
        protected void Page_Load(object sender, EventArgs e)
        {                           
                //pobranie ID kursu ktory ma być wyświetlony
                yourcourseid = Request.QueryString["yourcourseid"];

                //Ustawienie tytułu strony
                SqlDataSource1.SelectCommand = "SELECT CourseName FROM Courses WHERE Courses.CourseID =" + yourcourseid;

                DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
                String titleString = (String)dv.Table.Rows[0][0];
                TitleLabel.Text = titleString;

                //Pobranie nazw lekcji do dropDOwnList1
                SqlDataSource1.SelectCommand =
                    "SELECT CourseSectionID, SectionName FROM CourseSections WHERE CourseID =" + yourcourseid + " ORDER BY CourseSectionID";
                
                DropDownList1 = new DropDownList();
                DropDownList1.DataSource = SqlDataSource1;
                DropDownList1.DataTextField = "SectionName";
                DropDownList1.DataValueField = "CourseSectionID";
                DropDownList1.DataBind();
                PlaceHolder1.Controls.Add(DropDownList1);

                //Pobranie nazw testow do dropDOwnList2
                DropDownList2 = new DropDownList();
                DropDownList2.DataSource = SqlDataSource1;
                DropDownList2.DataTextField = "SectionName";
                DropDownList2.DataValueField = "CourseSectionID";
                DropDownList2.DataBind();
                PlaceHolder2.Controls.Add(DropDownList2);

                //Pobranie ID usera
                MembershipUser myObject = Membership.GetUser();
                string StudentID = myObject.ProviderUserKey.ToString();

                SqlDataSource1.SelectCommand = "SELECT QuizResults.Score, QuizResults.TotalQuestions, QuizResults.DateTaken, CourseSections.SectionName FROM QuizResults INNER JOIN CourseSections ON QuizResults.CourseSectionID = CourseSections.CourseSectionID INNER JOIN Courses ON CourseSections.CourseID = Courses.CourseID WHERE (Courses.CourseID = " + yourcourseid + ") AND (QuizResults.StudentID = '" + StudentID + "')";
                GridView1.DataSource = SqlDataSource1;
                GridView1.DataBind();
                                   
        }
        //Przejscie do nauczania danego kursu
        protected void Button1_Click(object sender, EventArgs e)
        {
            //Pobranie id sekcji kursu
            String section = DropDownList1.SelectedValue;            
            Response.Redirect("CourseText.aspx?yourcourseid=" + yourcourseid + "&section=" + section);

        }
        //Przejscie do testu danego kursu
        protected void Button2_Click(object sender, EventArgs e)
        {
            String section = DropDownList2.SelectedValue;
            Response.Redirect("CourseTest.aspx?yourcourseid=" + yourcourseid + "&section=" + section);
        }
    }
}
